package com.comum.javahomework420242025.controller;

import com.comum.javahomework420242025.mapper.ChanceAndClientMapper;
import com.comum.javahomework420242025.pojo.*;
import com.comum.javahomework420242025.repository.AllFileRepository;
import com.comum.javahomework420242025.repository.ChanceRepository;
import com.comum.javahomework420242025.repository.TraceUserRepository;
import com.comum.javahomework420242025.repository.WeeklyReportRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ChanceController {

    @Autowired
    private ChanceRepository chanceRepository;
    @Autowired
    private ChanceAndClientMapper chanceAndClientMapper;
    @Autowired
    private TraceUserRepository traceUserRepository;
    @PostMapping("/addChance")
    public Result addChance(@RequestBody Chance chance){

        System.out.println("chance = " + chance.toString());

        Chance newchance =  chanceRepository.save(chance);
        return new Result(0, "操作成功", newchance.getChanceid());
    }
    @PutMapping("/updateChance")
    public Result updateChance(@RequestBody Chance chance){
        chanceRepository.save(chance);
        return Result.success();
    }
    @DeleteMapping("/deleteChance")
    public Result deleteChance(String id){
        System.out.println(id);
        traceUserRepository.deleteByChanceId(Long.valueOf(id));
        chanceRepository.deleteById(Long.valueOf(id));

        return Result.success();
    }
    @PostMapping("/getChance")
    public Result<PageBean<Chance>> getChance(@RequestBody AskList askList){
        int pageSize = askList.getPageSize();
        int pageNum = askList.getPageNum();
        String str = askList.getStr();
        String findType = askList.getFindType();

        System.out.println("findType = " + findType);
        PageBean<Chance> pb = new PageBean<>();
        Pageable pageable = PageRequest.of(pageNum-1,pageSize);
        Page<Chance> page = chanceRepository.list(str, findType, pageable);
        pb.setTotal(page.getTotalElements());
        pb.setItems(page.getContent());
        System.out.println(pb);
        return Result.success(pb);
    }
    @PostMapping("/findByClue")
    public Result<PageBean<Chance>> findByClue(@RequestBody AskList askList){
        int pageSize = askList.getPageSize();
        int pageNum = askList.getPageNum();
        String str = askList.getStr();
        String findType = askList.getFindType();

        System.out.println("findType = " + findType);
        PageBean<Chance> pb = new PageBean<>();
        Pageable pageable = PageRequest.of(pageNum-1,pageSize);
        Page<Chance> page = chanceRepository.findByClue(str, findType, pageable);
        pb.setTotal(page.getTotalElements());
        pb.setItems(page.getContent());
        System.out.println(pb);
        return Result.success(pb);
    }

    @GetMapping("/getChanceAndClientForResult")
    public Result<List<ChanceAndClientForResult>>get(){
        List<ChanceAndClientForResult> res = chanceAndClientMapper.getAllChanceAndClient();
        return Result.success(res);
    }



    @GetMapping("/getAllChance")
    public Result getAllChance()
    {
        return Result.success(chanceRepository.findAll());
    }


    @PostMapping("/getAllChanceByClientId")
    public Result getAllChanceByClientId(@RequestBody AskList askList) {
        String str = askList.getStr();
        String findType = askList.getFindType();
        List<Chance> chances = chanceRepository.getAllChanceByClientId(str, findType);
        return Result.success(chances);
    }
}
